﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DungDungShit
{
    class Player : NonTile
    {
        int hp = 3;
        public int HP { get { return hp; } }
        bool invincible = false;
        int invTime = 0;
        List<Person> rescueList = new List<Person>();
        List<Person> rescued = new List<Person>();
        int maxCapacity = 3;
        int capacity = 1;
        int score = 0;
        int bonus = 0;
        DateTime limitTime = DateTime.Today.AddMinutes(3);

        public DateTime LimitTime { get { return limitTime; } }
        public int Score { get { return score; } }
        public List<Person> RescueList { get { return rescueList; } }
        public List<Person> Rescued { get { return rescued; } }
        public int Capacity { get { return capacity; } }
        public bool isFull()
        {
            return rescued.Count >= capacity;
        }
        public void Rescue(Person player)
        {
            if (isFull()) return;
            else rescued.Add(player);
        }
        public void Release()
        {
            foreach (Person p in rescued)
            {
                rescueList.Add(p);
                getPrize(p);
            }
            rescued.Clear();
        }
        public void getPrize(Person p)
        {
            score += p.BasicPrize();
            switch (p.Type)
            {
                // 용량
                case 0:
                    AddCapacity(p.Prize());
                    break;
                // 시간
                case 1:
                    AddTime(p.Prize());
                    break;
                // 돈
                case 2:
                    AddBonus(p.Prize());
                    break;
                // 체력
                case 3:
                    GetHealth(p.Prize());
                    break;
                default: 
                    return;
            }
        }

		public Player(Texture2D defaultTexture, Texture2D leftTexture, Texture2D rightTexture, Texture2D upTexture, Texture2D downTexture, IntVector2 position, IntVector2 frameSize, IntVector2 sheetSize)
			: base(defaultTexture, leftTexture, rightTexture, upTexture, downTexture, position, frameSize, sheetSize)
		{
			this.layerDepth = 0.4f;
		}
        public override int Speed { get { return 20; } }

        public override void Update(GameTime gameTime) {
            if (FrameCount - invTime > 15)
            {
                invTime = 0;
                invincible = false;
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 origin) {
            if (!invincible || (invincible && (FrameCount - invTime) % 2 == 1))
            {
                base.Draw(gameTime, spriteBatch, origin);
            }
        }

        public void GetDamage() {
            if (!invincible)
            {
                hp--;
                invincible = true;
                invTime = FrameCount;
            }
        }
        public void GetHealth(int i) { hp += i; }
        public void AddTime(int seconds)
        {
            limitTime = limitTime.AddSeconds((double)seconds);
        }
        public void AddScore(int i)
        {
            score += i;
        }
        public void AddBonus(int i)
        {
            bonus += i;
        }
        public void AddCapacity(int i)
        {
            capacity += i;
            if (capacity >= maxCapacity)
                capacity = maxCapacity;
        }

        public string FinalResult(bool death)
        {
            int sum = score + bonus;
            string result = "Results\nFinal Score : ";
            result += death ? score.ToString() : score + " + " + bonus + " = " + sum;
            result += "\nPeople Saved : " + rescueList.Count;
            return result;
        }
    }
}
